package com.gxtbindings.bindings.client;

import java.util.ArrayList;
import java.util.List;

/**
 * A collection of bindable objects that can be committed or rejected as a unit.
 * 
 * For instance, you may multiple BindingGroup's and a ListStoreBinding that you want
 * to commit or reject as a unit without writing this code multiple times.
 * 
 * @author Jonathan Janisch
 *
 */
public class CommitSet implements Bindable {

	private List<Bindable> bindings = new ArrayList<Bindable>();
	
	public void add(Bindable bindable)
	{
		bindings.add(bindable);
	}
	
	public void remove(Bindable bindable)
	{
		bindings.remove(bindable);
	}

	@Override
	public void commitChanges() {
		for (Bindable binding : bindings)
		{
			binding.commitChanges();
		}
	}

	@Override
	public void rejectChanges() {
		for (Bindable binding : bindings)
		{
			binding.rejectChanges();
		}
	}
	
	@Override
	public void unbind() {
		for (Bindable binding : bindings)
		{
			binding.unbind();
		}
	}
	
	@Override
	public boolean hasChanges() {
		for (Bindable binding : bindings)
		{
			if (binding.hasChanges())
			{
				return true;
			}
		}
		
		return false;
	}
}
